require('../css/mine-edit.less')
// import $ from 'jquery'
// 入口函数
document.ready(function () {
  const person = {
    nickname: '',
    gender: '',
    birthday: '',
    city: '',
    sign: ''
  }
  // 1.0 点击返回
  const backBox = document.querySelector('#back')
  backBox.addEventListener('click', function () {
    history.back()
  })

  // 2.0 选择性别
  const showPicker = document.querySelector('#showPicker')
  showPicker.addEventListener('click', function () {
    weui.picker([{
      label: '男',
      value: 0
    }, {
      label: '女',
      value: 1
    }], {
      //确认之后回触发这个事件
      onConfirm: function (result) {
        //存储性别的值
        person.gender = result[0].label
        showPicker.textContent = result[0].label
      }
    })
  })

  // 3.0 生日选择
  const showDatePicker = document.querySelector('#showDatePicker')
  const birthday = document.querySelector("#birthday")
  showDatePicker.addEventListener('click', function () {
    weui.datePicker({
      start: 1990,
      end: new Date().getFullYear(),
      onConfirm: function (result) {
        let str = ''
        result.forEach(function (item) {
          str += item.label
        })
        person.birthday = str
        birthday.textContent = str
      }
    })
  })

  //选择省份
  const showCityPicker = document.querySelector("#showCityPicker");
  const cityData = document.querySelector("#cityData")
  showCityPicker.addEventListener('click', function () {
    //先发请求，获取所有的省份
    window.$http.get("/address/province", function (res) {
      const arr = [];
      if (res.status === 0) {
        res.data.forEach(function (item) {
          arr.push({
            label: item.name,
            value: item.addressId
          })
        })

        //遍历整理好了数据之后再渲染列表选择
        weui.picker(arr, {
          //确认之后回触发这个事件
          onConfirm: function (data1) {
            //存储省份的值
            // console.log('省份是',data1);
            window.$http.get("/address/city/" + data1[0].value, function (res) {
              // console.log(res);//对应省份的市区
              const arr1 = []
              if (res.status === 0) {
                res.data.forEach(function (item) {
                  arr1.push({
                    label: item.name,
                    value: item.addressId
                  })
                })
                setTimeout(function () {
                  weui.picker(arr1, {
                    //确认之后回触发这个事件
                    onConfirm: function (data2) {
                      console.log('省份是', data1);
                      console.log('市区是', data2);
                      cityData.textContent = data1[0].label + data2[0].label
                      person.city = [data1[0].label, data2[0].label]
                    }
                  })
                }, 500)
              }
            })
          }
        })
      }
    })
  })

  //监听文本域的键盘输入事件
  let signBox = document.querySelector("#sign_box")
  let signNum = document.querySelector("#sign_num")
  signBox.addEventListener("input", function () {
    signNum.textContent = this.value.length
  })

  
  //点击保存个人信息
  const saveInfo = document.querySelector("#saveInfo")
  let jsInput = document.querySelector("#js_input")
  let userId = JSON.parse(window.localStorage.getItem('user')).userId
  saveInfo.addEventListener("click", function () {
    let jsInputVal = jsInput.value
    let signBoxVal = signBox.value

    let newStr = person.birthday;
    newStr = newStr.slice(0, newStr.length - 2);

    newStr = newStr.split(/[\u4e00-\u9fa5]/);

    let birthArr = [];
   
    newStr.forEach(function (item) {
      if (Number(item) < 10) {
        item = '0' + item;
      }
      birthArr.push(item);
    })

    birthArr = birthArr.join('-');

    window.$http.post('/users/userEdit', {
      nickname: jsInputVal,
      gender: person.gender,
      birthday: birthArr,
      address: person.city,
      sign: signBoxVal,
      userId: userId
    }, function (res) {
      console.log(res);
      if (res.status === 0) {
        window.$utils.toast(res.status, res.msg)
        location.href="./mine.html"
      }
    })
    
  })
  window.$http.get("/users/accountinfo?userId=" + userId, function (res) {
    console.log(res);
    console.log(res);
      jsInput.value=res.data.nickname;
    
      signBox.value=res.data.sign;

      showPicker.textContent=res.data.gender;

      birthday.textContent=res.data.birthday.slice(0, 10);

      cityData.textContent=res.data.address;
      signNum.textContent=signBox.value.length;
    console.log(res.data.imgurl);
  })

})

